package com.ede.roverhu.simplebackstage.Utils;

import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * Created by Rover on 2016/10/10 .
 * Email 357319517@qq.com
 * <p/>
 * 字符转换
 */
public class StringConv {
    //Unicode转UTF-8
    public static String Unicode2UTF8(String s) {
        byte[] bytes = new byte[0];
        try {
            bytes = s.getBytes("UTF-8");
            return new String(bytes, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "未知错误";
        }
    }

    //URL转UTF-8
    public static String URLcode2UTF8(String s) {
        try {
            return URLDecoder.decode(s, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    //String 转urlcode
    public static String String2URLcode(String s) {
        try {
            return URLEncoder.encode(s, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    //sp字符串裁剪
    public static String sp2normal(String s) {
        LogUtils.d(s);
        if (s != null) {
            s = s.substring(s.indexOf("=") + 1, s.indexOf(";"));
            return URLcode2UTF8(s);
        } else {
            return "";
        }

    }
}
